/*
 * Zegar gry, tyka co pewien czas i uruchamia odpowiednie elementy gry
 * (wykrywanie kolizji, przesuwanie obiektów itp.)
 */
#include "Clock.h"
#include "WindowManager.h"
#include <allegro.h>

Clock::Clock(Tank *t1, Tank *t2)
{
	dm = DisplayManager::getInstance();
	dm->init(WindowManager::WIDTH, WindowManager::HEIGHT);
	
	tankOne = t1;
	tankTwo = t2;
	
	bulletBmp = load_bitmap("res/img/standard/bullet.bmp", NULL);
	
	dm->add(tankOne);
	dm->add(tankTwo);
}

Clock::~Clock()
{
}

void Clock::tick()
{
	Bullet *bullet;
	if(key[KEY_LEFT]) tankOne->move(Direction::WEST);
	if(key[KEY_RIGHT]) tankOne->move(Direction::EAST);
	if(key[KEY_UP]) tankOne->move(Direction::NORTH);
	if(key[KEY_DOWN]) tankOne->move(Direction::SOUTH);
	if(key[KEY_RCONTROL])
	{
		if((bullet = tankOne->shoot(bulletBmp)) != NULL)
		{
			registerBullet(bullet);
			dm->add(bullet);
		}
	}

	if(key[KEY_A]) tankTwo->move(Direction::WEST);
	if(key[KEY_D]) tankTwo->move(Direction::EAST);
	if(key[KEY_W]) tankTwo->move(Direction::NORTH);
	if(key[KEY_S]) tankTwo->move(Direction::SOUTH);
	if(key[KEY_SPACE])
	{
		if((bullet = tankTwo->shoot(bulletBmp)) != NULL)
		{
			registerBullet(bullet);
			dm->add(bullet);
		}
	}
	
	moveBullets();
	dm->render();
	clear_keybuf();
}

void Clock::registerBullet(Bullet *bullet)
{
	bullets.push_back(bullet);
}

void Clock::moveBullets()
{
	vector<Bullet *>::iterator it;
	for (it = bullets.begin(); it < bullets.end(); it++)
	{
		(*it)->move();
	}
}

